Towards Model Driven Architecture and Painless Persistence with Airlift, UML and Hibernate


Milan Zimmermann, Airlift LLC


Last revision September 20, 2005



Summary: Benefits of a “data model first” approach to development are well established. In many projects we worked on, we have encountered a need for a consistent domain model driven approach with the requirement of the persistence to be a relational database. While many technologies (Hibernate, EJB) address this problem, they do not provide a seamless easy way to drive the development process from a Domain Entity Model (DEM) designed in UML all the way to persistence. Hibernate is an excellent Object-Relational(OR) mapping tool, however, the task of creating and managing details of Hibernate OR mapping XML and corresponding Java classes may become overhelming for large projects. The LGPL Airlift framework provides, among other features, a Model Driven Approach (MDA) and tools which allow to design the Domain Entity Model in UML, including inheritance, generate all Hibernate OR mapping details, and support evolution of the Entity Model by separating any generated classes and interfaces from business classes and interfaces.

Available formats:

HTML
PDF
doc
Open Office 2.0

Fast Introduction: The goal of this tutorial is to present the Airlift library and tools which support MDA development, from UML Domain Entity Model to persistence and business logic. Let us start by jumping ahead and taking a look at the AirliftPetStore Entity Model (Customers and PurchaseOrders class diagrams). A few notes:


Tutorial Content: In this tutorial, we will build a small subset of the above AirliftPetStore UML Entity Model, and illustrate the Airlift MDA approach by generating all java and Hibernate classes using the AirliftUMLConverter, creating the target database using Airlift Hibernate tools, and coding a simple no-gui test application against the target database.

Tutorial Steps:


Towards Model Driven Architecture and Painless Persistence with Airlift, UML and Hibernate 1

Tutorial Steps: 2
Introduction to the environment: UML and persistence tools and libraries currently supported by Airlift. 2
Airlift plans to support following tools: 2
Installing software and tools used in this tutorial. 3
Core Section: Building the AirliftPetstoreSubset UML using the Airlift UML Profile. 4
Steps to build AirliftPetstoreSubset: 4
Detail description of tags supported by the Airlift UML Convertor. 10
Using the Airlift UML Convertor to generate java classes, interfaces and Hibernate XML mapping files, overview of generated code. 10
Running the Airlift UML Convertor 11
Overview of generated code 12
Running the Airlift Deploy Tool to create the target database. 13
Coding and running a small Junit test application against the AirliftPetstoreSubset domain entity model. 15
Conclusion: 17
Future Development: 17

Introduction to the environment: UML and persistence tools and libraries currently supported by Airlift.


In this section we introduce the tools tested and currently supported by Airlift.

Airlift plans to support following tools:


At this point, following tools have been tested and used by customers:
Any tool which allows saving the UML diagram in XMI 1.1 should work. Currently, Airlift users use MagicDraw version 9.0 and 9.5 (http://www.nomagic.com), saving the UML in XMI 1.1 “rich” format, which is the only combination that has been tested. Poseidon (http://www.gentleware.com) may work but was not tested. ArgoUML (http://argouml.tigris.org/) is currently not supported because the highest version of XMI it can save is 1.0. We want to support an open source or free source UML tool and looking into ArgoUML and Omondo/EclipseUML (http://www.omondo.com/download/free/eclipse_3x/index.html#E3.JAR) but need to clarify EclipseUML license.

Generating EJB and JDO is planned.

We would like to extend the tool support as quickly as possible and help anyone in testing and extending list of source and target tools, mainly free source and open source tools.

Installing software and tools used in this tutorial.


In this section, we list the tools used in this tutorial and provide download links so reader can follow this tutorial hands on.

However, this tutorial provides:


Consequently, you only need to install the tools below if you wish to follow this tutorial “hands-on” step by step.

External tools:


To follow this tutorial “hands-on” step by step, you will need to install:


Tutorial Software:



Core Section: Building the AirliftPetstoreSubset UML using the Airlift UML Profile.


In this section, we describe how to build the Domain Entity Model for AirliftPetstoreSubset. This is a small subset of the Airlift Petstore linked as an image in “Fast Introduction”.

Steps to build AirliftPetstoreSubset:




Having created the domain entity model, we will first (briefly) discuss Airlift UML tags and stereotypes, and then use the Airlift UML Converter to convert the entity model into Java code and Hibernate mappings.



Detail description of tags supported by the Airlift UML Convertor.


In this section, we will discuss Tags supported by the Airlift UML Converter. Airlift UML tags and stereotypes are defined in the AirliftUMLProfile.xml.zip. In the above model, we have seen tags such as “reverse” which manages association in reverse direction, and “cascadeTo” which manages how the “To” entity is being deleted on the “From” entity delete.

In general, with UML tags, designer can control association accessor types (Set, List, Map), concrete column names for foreign keys, cascading delete, lazy loading and other OR mapping features and features supported by Hibernate. While none of these tags are required and reasonable defaults are provided by either Airlift or Hibernate, tags are a powerful feature providing fine control.

All Airlift UML tags and stereotypes, along with their description, default and allowed values are listed in the Tags documentation. Please review it for details of supported tags.

Using the Airlift UML Convertor to generate java classes, interfaces and Hibernate XML mapping files, overview of generated code.


In this section, we will explain how to use the Airlift UML Convertor to convert UML to source code, overview the generated code, and discuss role of each generated class, as well as how this model supports maintaining your business changes.

The Airlift UML Convertor (Hibernate target) is the utility which takes the UML file (XMI 1.1 format), such as the AirliftPetstoreSubset we built in previous steps, and converts it into a set of Java files and Hibernate mapping files, which support persistence in the Airlift framework. This utility, along with Airlift persistence classes, is the core that allows for applications using Airlift to be based on a Model Driven approach to development.

Running the Airlift UML Convertor


The utility can be run by running “main” of utility class airliftj.util.entityModelUMLToXML.UMLEntities2Hibernate, found in the airlift_src.jar in the eclipse-workspace of this tutorial. This utility suports command line arguments that allow to specify the location of the UML file, location of the generated files, as well as properties which control the generated code (for example, whether the target relational tables will use char or varchar). This utility will be soon run from Eclipse plugin, so for now we will concentrate on describing a most common arguments.

For those following the tutorial step-by-step in Eclipse:


Overview of generated code


For convenience of a quick review, the generated Java classes and Hibernate XML mapping were copied into “tutorial/src” directory. For those following hands-on they are in the eclipse-workspace as well.

Each UML Class has been converted into 4 java files and 1 Hibenate mapping “.hbm.xml” file (there is no “hbm.xml” file generated for classes that have a superclass, the superclass’s “hbm.xml” file contains all mappings. As an example, let us look at the UML Person class. Here is description of each generated file and it’s role in the Airlift framework.


Running the Airlift Deploy Tool to create the target database.


In this section, we will explain what is the Airlift Entity Model Deployment Tool and how to use it.

Airlift Entity Model Deployment tool is a utility (soon to be generated , you can check the source code in PetstoreDeployTool.java) which can create all database tables corresponding to entities defined in _registry_allEntities.java (which in turn correspond to all Classes and Associations in the UML entity model. It is a wrapper using Hibernate utilities.

The Deploy Tool must run at least once before running any application that uses the database as modelled in the UML converted in previous steps.

Configuration Files required to run the Deployment Tool:


Java files required to run the Deployment Tool:


If you are following the tutorial step by step, you can run the deployment tool:

id bigint
emailAddress varchar
title varchar
suffix varchar
firstName varchar
middleName varchar
lastName varchar
emailNewsletter tinyint
registered date
bannerPreference varchar
supplierCompanyName varchar
myListPreference varchar
preferredLanguage varchar
updateSequence int
CONCRETE_TYPE varchar

Creating the database tables concludes discussion of the Airlift Entity Model Deploy Tool, and we will proceed writing a simple application against the Entity Model.

Coding and running a small Junit test application against the AirliftPetstoreSubset domain entity model.


In this section we will create an exteremely simple application (a test) that will show how data can be added and accessed in our Domain Entity Model, as generated from UML into Airlift entities as persisted by Hibernate.

We create a simple unit test application which will create and persist 3 persons with addresses. For those following step by step, this test can be run as follows:
When finished, you should see 3 rows in the “person” table and 3 rows in the “address” table (associated via address.PERSON_ID).

A few notes about the test classes and how they show persistence entity management in Airlift

public void testPersonQuery_LoadTestData()
{
ResponseSummary response =
this.getConversation().executeDemarcationBlock(new DemarcationBlock()
{
public void executeWithDemarcation()
{
insertPerson(smith, "smith@airliftj.org", "Tennesee", "Nashville");
insertPerson(miller, "miller@airliftj.org", "Alabama","Birmingham");
insertPerson(carpenter, "carpenter@airliftj.org", "Ontario", "Oakville");
} // NOTE: automatic commit happens at the end of this block..
// or rollback if an exception was thrown inside this block
// this assumes of course, that there was no transaction
//already running when we got here.
},DemarcationSettings.SETTINGS_TRANSACTION_REQUIRED);
}
}

private void insertPerson(String lastName, String email, String state, String city)
{
// delete any Person in database by name
while(deletePersonIfExists(lastName));
Person person = (Person)getEntityManager()
.createEntityAndAssignIdentity(Person.class);
person.setLastName(lastName);
person.setEmailAddress(email);
getTransactionManager().managePersistentEntity(person);
// Create a single Address for this Person.
Address address =(Address)getEntityManager()
.createEntityAndAssignIdentity(Address.class);
address.setState(state);
address.setCity(city);
Set addresses = new HashSet();
addresses.add(address);
person.setAddress(addresses);
getTransactionManager().managePersistentEntity(address);
}


Conclusion:


This tutorial attempted to present a complete MDA based development cycle using the Airlift framework, from a EntityModel defined in UML, through fully automated Object Relational mapping, all the way to writing an application and persisting data against the Entity Model. Perhaps one day, we will be developing in (something similar to) UML as envisioned by the MVC inventor Trygve Reenskaug in his BabyUML research , in the meantime, a few small steps.



Future Development:


Plans for future is in general terms, increased pluggability on both UML and persistent ends, and making use of at least following tools:

ArgoUML front end
EJB back end
JDO back end